<!--
* @Author: 冯尚新 fengshangxin@yunjinhz.com
* @Date:  2024-05-10 19:37:44
* @FilePath: /dev/pages/store/box/addStoreGoods.vue
* @Description: 封箱码物品添加列表
-->

<template>
	<view>
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
		</theme-navbar>
		<!-- #endif -->
		<view class="box-search-lay" :style="{'top': isHideNav ? 0 : '44px'}">
			<u-search shape="round" :clearabled="true" :showAction="true" actionText="确定" :animation="true"
				placeholder="" v-model="searchKey" @custom="toSearchList"
				:disabled="detailInfo.formTo == 'bill' ? true: false "></u-search>
		</view>

		<view class="content u-page">
			<u-checkbox-group>
				<me-table :i="0" :index="0" :top="112" :load-data="loadData" :height="getListViewHeight()"
					:init-load="false" ref="mescrollItem0">
					<view slot-scope="d" class="list-item-root form-style">
						<addStoreGoodItem :item-info="d.data"></addStoreGoodItem>
					</view>
				</me-table>
			</u-checkbox-group>
		</view>

		<view class="submit-btns btn-other-cls">
			<u-button class="u-btn" type="primary" @click="sumbitStoreBox">
				提交
			</u-button>
		</view>

	</view>
</template>

<script>
	import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
	import meTable from "@/components/me-table.vue";
	import addStoreGoodItem from "./../components/addStoreGoodItem.vue";
	import storeService from '@/service/store/store.service.js';

	export default {
		components: {
			meTable,
			addStoreGoodItem
		},
		mixins: [MescrollMoreMixin, ],
		data() {
			return {
				searchKey: "",
				detailInfo: {},
			};
		},
		onLoad() {
			this.detailInfo = this.getRouteParam();
			this.searchKey = this.detailInfo.docName;
		},
		methods: {
			getListViewHeight() {
				let p = this.$u.sys().windowHeight - 44 - 48 - uni.upx2px(10) - this.$u.sys().statusBarHeight + "px";
				// #ifdef H5-DINGDING || H5-WECHAT
				p = this.$u.sys().windowHeight - uni.upx2px(10) - 48 - this.$u.sys().statusBarHeight + "px";
				// #endif
				return p;
			},
			loadData(page) {
				let parmes = {
					page: page.num,
					limit: page.size,
					paras: {
						storeBill:this.searchKey,
						caseCode:this.searchKey
						// storeBill: this.detailInfo.storeBill
					}
				}
				return new Promise((resolve, reject) => {
					storeService.queryGoodsByBillCodeList(parmes).then((res) => {
						if (res.code == 0 && res.success) {
							let arr = res.data;
							// 进步条最大值不能 超过实际封箱条数 
							arr && arr.forEach(item => {
								// 处理 surplusQty 字段为 undefined 问题
								if (!item.surplusQty) {
									item.surplusQty = 0;
								}
								item.maxSurplusQty = item.surplusQty
							});

							resolve({
								count: res.count,
								data: arr,
							})
						} else {
							this.$u.toast("请求失败:" + res.msg);
							resolve({
								count: 0,
								data: [],
							});
						}
					}).catch((err) => {
						this.$u.toast("请求失败:" + err);
						resolve({
							count: 0,
							data: [],
						});
					})
				})
			},
			sumbitStoreBox() {
				// 1、判断是否勾选了物品
				let data = this.$refs.mescrollItem0.listDatas.filter(item => {
					if (item.checked) return item
				});
				// 2、没有勾选物品提示
				if (data.length == 0) return this.$modal.alert("请勾选物品");
				// 3、判断 实际封箱条数不能为0
				let str = "";
				let flag = false;
				data.forEach((item, index) => {
					if (item.surplusQty <= 0) {
						str += `第 ${index+1} 行物品实际封箱条数不能为0。\n`;
						flag = true;
					}
				})
				// 4、 实际封箱条数为0 提示
				if (flag) return this.$modal.alert(str);
				// 5、组装提交的数据
				data = data.filter(item => {
					if (item.checked && item.surplusQty > 0) return item
				});
				console.log(data);
				this.addStoreGoodToServe(data)
			},
			// 保存封箱码 物品信息
			addStoreGoodToServe(data) {
				this.$modal.showLoading('正在保存...');
				let params = {
					storeBill: this.detailInfo.storeBill,
					storeId: this.detailInfo.storeId || "",
					psCode: this.detailInfo.psCode || "",
					storeName: this.detailInfo.storeName || "",
					qrCode: this.detailInfo.qrCode || "",
					list: data
				}
				console.log(params)

				storeService.addSealGoodList(params).then((res) => {
					this.$modal.hideLoading();
					if (res.code == 200 && res.success) {
						this.backPrev({
							paramKey: 'refresh',
							param: true,
						})
					} else {
						this.$u.toast(res.msg);
					}
				}).catch((err) => {
					this.$modal.hideLoading();
				})
			},
			toSearchList() {
				this.getMescroll(0).triggerDownScroll();
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background-color: #f3f4f5;
		min-height: calc(100vh - 46px);
		padding-bottom: 160rpx;
	}

	.box-search-lay {
		background-color: #fff;
		position: fixed;
		// #ifndef H5-DINGDING
		top: 44px;
		// #endif
		// #ifdef H5-DINGDING
		top: 0px;
		// #endif
		z-index: 999;
		width: 100%;
		overflow: hidden;
		padding: $cm-search-padding-row $cm-search-padding-column;
	}

	.key-search {
		width: 100%;
		margin: auto;
		padding-left: 2.5%;
		padding-right: 2.5%;
		line-height: 48px;
	}

	.list-item-root {
		margin: 10px 0;
		overflow: hidden;
		background: #FFFFFF;
	}

	.fs-30 {
		font-size: 30rpx
	}

	.form-style {
		padding: 0rpx 8rpx
	}

	.form-style-title {
		display: flex;
		justify-content: flex-start;
		flex-direction: row;
		width: 100%;
		align-items: flex-start;
	}

	/deep/ .u-checkbox-group {
		display: contents;
	}
</style>